﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KNXObjects
{
    public class AngleObject : GenericObjectBase<UInt32>
    {
        #region ObjectBase Implementations

        public override string DataPointEISName
        {
            get { return string.Empty; }
        }

        public override string DataPointID
        {
            get { return "5.003"; }
        }

        public override string ToString()
        {
            return ((float)Value * 360 / 256).ToString("#.####");
        }

        public override uint Value
        {
            set
            {
                if (value > 360)
                    throw new ApplicationException("AngleObjectValue: Value must be between 0 and 360. Val : " + value);
                base.Value = (uint)(((int)(value * 256 / 360)) % 256);
            }
        }

        public override void SetValue(string value)
        {
            UInt32 val;
            if (!UInt32.TryParse(value, out val))
                throw new ApplicationException(string.Format("Format error, cannot convert value {0} to UINT32", value));
            Value = val;

        }

        #endregion

        #region ctor

        public AngleObject(string id, string address, string name, string flags)
            : base(id, address, name, flags)
        {
        }
        public AngleObject(string value)
        {
            SetValue(value);
        }
        
        public AngleObject(UInt32 val)
        {
            Value = val;
        }
        #endregion
    }
}